﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// xinxi 的摘要说明
    /// </summary>
    public class xinxi : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;
        protected int quanzi_id = 1;
        protected int type_id = 0;
        public void ProcessRequest(HttpContext context)
        {
            this.pageSize = 15;
            this.page = DTRequest.GetQueryInt("page", 1);
            this.quanzi_id = DTRequest.GetQueryInt("quanzi_id", 1);
            this.type_id = DTRequest.GetFormInt("type_id", 0);

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.xinxi bll = new BLL.xinxi();
            string strWhere = "id>0";
            if (this.quanzi_id > 0)
            {
                strWhere += " and quanzi_id=" + this.quanzi_id;
            }
            if (this.type_id > 0)
            {
                strWhere += " and type_id=" + this.type_id;
            }
            DataSet ds = bll.GetList(this.pageSize, this.page, strWhere, "add_time desc", out this.totalCount);
            int total_page = this.totalCount / this.pageSize;
            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"xinxi_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr3 = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr3["id"]);
                    strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(dr3["content"].ToString()) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr3["add_time"])) + "\"");
                    strTxt.Append(",\"hits\":" + dr3["hits"]);
                    strTxt.Append(",\"user_id\":" + dr3["user_id"]);
                    if (Convert.ToInt32(dr3["user_id"]) > 0)
                    {
                        string _username = new BLL.users().GetUserName(Convert.ToInt32(dr3["user_id"]));
                        string _avatar = new BLL.users().GetAvatar(Convert.ToInt32(dr3["user_id"]));
                        strTxt.Append(",\"user_name\":\"" + _username + "\"");
                        if (string.IsNullOrEmpty(_avatar))
                        {
                            strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                        }
                        else
                        {
                            if (_avatar.Contains("shicaiquanzi"))
                            {
                                strTxt.Append(",\"avatar\":\"http://" + _avatar + "\"");
                            }
                            else
                            {
                                strTxt.Append(",\"avatar\":\"http://www.shicaiquanzi.com" + _avatar + "\"");
                            }
                        }
                    }

                    BLL.xinxi_albums pbll = new BLL.xinxi_albums();
                    DataSet pds = pbll.GetLists(int.Parse(dr3["id"].ToString()));
                    if (pds.Tables[0].Rows.Count > 0)
                    {
                        strTxt.Append(",\"pic_list\":[");
                        for (int ii = 0; ii < pds.Tables[0].Rows.Count; ii++)
                        {
                            DataRow pdr = pds.Tables[0].Rows[ii];
                            string picurl = pdr["thumb_path"].ToString();
                            if (picurl.Contains("shicaiquanzi.com"))
                            {
                                strTxt.Append("{\"picurl\":\"http://" + picurl + "\"}");
                            }
                            else
                            {
                                strTxt.Append("{\"picurl\":\"http://www.shicaiquanzi.com" + pdr["thumb_path"] + "\"}");
                            }
                            //是否加逗号
                            if (ii < pds.Tables[0].Rows.Count - 1)
                            {
                                strTxt.Append(",");
                            }
                        }
                        strTxt.Append("]");
                    }

                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"status\":2,\"xinxi_list\":[]");
            }
            strTxt.Append("}");

            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}